Ext.define('Intranet.VeCongTy.view.AdminView', {
    extend: 'Ext.panel.Panel',
    alias: 'widget.VeCongTy_AdminView',
    layout: {
        type: 'hbox',
        align: 'stretch'
    },
    items: [{
            xtype: 'tabpanel',
            id: 'VeCongTy_AdminView_TabPanel',
            flex: 1,
            plain: true,
            items: [{
                    title: 'So do to chuc',
                    id: 'VeCongTy_so_do',
                    tabConfig: {
                        tooltip: 'A button tooltip'
                    }

                }]
        }],
    bbar: [{
            xtype: 'button',
            text: 'Add new tab',
            handler: function(btn) {
                Ext.create('Ext.window.Window', {
                    title: 'Add tab',
                    layout: 'fit',
                    id: 'VeCongTy_AdminView_AddTabWindow',
                    items: {
                        xtype: 'VeCongTy_AddTab'
                    }
                }).show();
            }

        }, '->', {
            xtype: 'button',
            text: 'Remove tab',
            handler: function(btn) {
                var panel = Ext.getCmp('VeCongTy_AdminView_TabPanel');

                var activeTab = panel.getActiveTab();
                var key_name = activeTab.getId().replace('VeCongTy_', '');

                if (key_name === 'so_do')
                {
                    Ext.Msg.alert('Warning', 'This tab cannot be removed!');
                }
                else
                {

                    Ext.Ajax.request({
                        url: '/src/public/ve-cong-ty/tab/removetab',
                        params: {
                            'key_name': key_name
                        },
                        success: function(response, opts) {
                            var data = Ext.decode(response.responseText);

                            if (data.success === 'true')
                            {
                                Ext.Msg.alert('Remove', 'Remove successful!');
                                var panel = Ext.getCmp('VeCongTy_AdminView_TabPanel');
                                panel.remove('VeCongTy_' + opts.params.key_name);
                            }

                        }
                    });
                }
            }
        }],
    initComponent: function()
    {
        this.callParent(arguments);

        Ext.Ajax.request({
            url: '/src/public/ve-cong-ty/tab/index',
            headers: {
                'Accept': 'application/json',
                'charset': 'utf-8'
            },
            success: function(response, opts) {
                var data = Ext.decode(response.responseText);

                if (data.success === 'true')
                {
                    var panel = Ext.getCmp('VeCongTy_AdminView_TabPanel');

                    var tabs = data.tabs;

                    for (var i = 0; i < tabs.length; i++)
                    {

                        var newtab = Ext.create('Ext.panel.Panel', {
                            title: tabs[i].name,
                            id: 'VeCongTy_' + tabs[i].key_name,
                            tabConfig: {
                                tooltip: tabs[i].name
                            },
                            layout: 'fit',
                            items: [{
                                    xtype: 'htmleditor',
                                    enableColors: false,
                                    enableAlignments: false,
                                    flex: 1,
                                    value: tabs[i].value
                                }],
                            bbar: ['->', {
                                    xtype: 'button',
                                    text: 'Save',
                                    margin: '0 40 0 0',
                                    handler: function(btn) {
                                        var parent_panel = this.up('panel');
                                        var key_name = parent_panel.getId().replace('VeCongTy_', '');

                                        Ext.Ajax.request({
                                            url: '/src/public/ve-cong-ty/tab/savevalue',
                                            params: {
                                                key_name: key_name,
                                                value: parent_panel.items.getAt(0).getValue()
                                            },
                                            success: function(response, opts) {
                                                var obj = Ext.decode(response.responseText);

                                                if (obj.success === 'true')
                                                {
                                                    Ext.Msg.alert('Successful', 'Save successful!');
                                                }
                                            },
                                            failure: function(response, opts) {
                                                Ext.Msg.alert('Error:', response.status);
                                            }

                                        });
                                    }
                                }]


                        });

                        panel.add(newtab);

                    }
                }
                else
                {
                    Ext.Msg.alert('Error:', obj.msg);
                }

            },
            failure: function(response, opts) {
                console.log('Add tab failed: ' + response.status);
            }
        });
    }
});
